package com.jtech.together.mvp.presenter;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.jtech.together.manager.SettingManager;
import com.jtech.together.manager.UserManager;
import com.jtech.together.model.FriendModel;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.UserProfileContract;
import com.jtech.together.util.BmobUtil;
import com.jtech.together.util.IM;
import com.jtech.together.view.activity.UserProfileActivity;

import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * 用户信息页面
 * Created by wuxubaiyang on 2017/5/11.
 */
public class UserProfilePresenter implements UserProfileContract.Presenter {
    private Context context;
    private UserProfileContract.View view;

    private UserModel userModel;
    private boolean isFriend;
    private String objectId;

    public UserProfilePresenter(Context context, UserProfileContract.View view, Bundle bundle) {
        this.context = context;
        this.view = view;
        //获取用户id
        if (null != bundle) {
            this.objectId = bundle.getString(UserProfileActivity.KEY_USER_OBJECT_ID, "");
        }
    }

    @Override
    public boolean isFriend() {
        return isFriend;
    }

    @Override
    public String getObjectId() {
        return objectId;
    }

    @Override
    public boolean isMine() {
        return objectId.equals(UserManager.getCurrentUser().getObjectId());
    }

    @Override
    public void loadUserProfile() {
        if (TextUtils.isEmpty(objectId)) {
            view.loadFail("用户信息加载失败");
            return;
        }
        //请求用户信息
        new BmobQuery<UserModel>()
                .getObject(objectId, new QueryListener<UserModel>() {
                    @Override
                    public void done(UserModel userModel, BmobException e) {
                        if (null == e) {
                            view.loadSuccess(userModel);
                            //存储用户信息
                            UserProfilePresenter.this.userModel = userModel;
                            //转化并存储用户信息
                            UserManager.updateIMUserInfo(userModel);
                        } else {
                            view.loadFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }

    @Override
    public void loadIsFriend() {
        if (TextUtils.isEmpty(objectId)) {
            view.isFriendFail("用户信息加载失败");
            return;
        }
        if (isMine()) {
            view.isFriend(false);
            return;
        }
        new BmobQuery<FriendModel>()
                .addWhereEqualTo("user", UserManager.getCurrentUser())
                .addWhereEqualTo("friendUser", new UserModel(objectId))
                .findObjects(new FindListener<FriendModel>() {
                    @Override
                    public void done(List<FriendModel> list, BmobException e) {
                        if (null == e) {
                            isFriend = list.size() > 0;
                            view.isFriend(isFriend);
                        } else {
                            view.isFriendFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }

    @Override
    public void addFriend(String message) {
        IM.build(UserManager.parseUserInfo(userModel), null)
                .sendAddFriendMessage(message, new MessageSendListener() {
                    @Override
                    public void done(BmobIMMessage msg, BmobException e) {
                        if (e == null) {//发送成功
                            view.sendInviteSuccess();
                            //发送成功则标记状态为已发送
                            SettingManager.get(context).flagSendAddFriendMessage(objectId);
                            //删除本地这条会话
                            BmobIM.getInstance().deleteConversation(msg.getConversationId());
                        } else {//发送失败
                            view.sendInviteFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }
}